package com.example.admin_beautiful.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Knife4jConfiguration {

    @Bean
    public Docket docket() {
//        // 配置全局请求头参数
        ParameterBuilder tokenParam = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        tokenParam.name("Authorization") // 请求头名称
                .description("认证令牌") // 描述
                .modelRef(new ModelRef("String")) // 数据类型
                .parameterType("header") // 参数类型
                .required(true) // 是否必填
                .defaultValue("admin")//默认值
                .build();

        parameters.add(tokenParam.build());

        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                //设置文档信息
                .apiInfo(apiInfo())
                //分组名称
                .groupName("1.0版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.admin_beautiful.controller"))
                .paths(PathSelectors.any())
                //添加全局请求头参数
                .build()
                .globalOperationParameters(parameters);
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //标题
                .title("beautiful接口文档")
                //描述
                .description("这是一个小程序项目")
                //服务条款URL
                .termsOfServiceUrl("http://www.xxx.com/")
                //作者
                .contact(new Contact("飞鼠","http://localhost:8080/","xxxxxxx@163.com"))
                //许可证URL
                .licenseUrl("....")
                //版本
                .version("1.0")
                .build();
    }
}

